package com.ihm.projetihm_v1.image.fragmentManager;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ihm.projetihm_v1.R;
import com.ihm.projetihm_v1.activity.main.ImageDownloader;
import com.ihm.projetihm_v1.nf.Graphe;
import com.ihm.projetihm_v1.nf.LinkedPhoto;

public class ImageListAdapter
		extends ArrayAdapter<Graphe<LinkedPhoto>>
{
	//+++++++++++//
	// CONSTANTS //
	//+++++++++++//

	//++++++++++++//
	// ATTRIBUTES //
	//++++++++++++//

	private int mResource;
	private boolean isMainActivity;
	private ImageFragment.MY_TABS mTab;
	

	//++++++++++++++//
	// CONSTRUCTORS //
	//++++++++++++++//

	public ImageListAdapter(
			Context context,
			int resourceId,
			List<Graphe<LinkedPhoto>> list,
			ImageFragment.MY_TABS tab, boolean isMainActivity)
	{
		super(context, resourceId, list);

        this.mResource = resourceId;
        this.mTab = tab;
        this.isMainActivity = isMainActivity;
	}
	
	
	//+++++++++//
	// METHODS //
	//+++++++++//
	
	@Override
	public View getView(
			int position,
			View convertView,
			ViewGroup parent)
	{
        Graphe<LinkedPhoto> item = getItem(position);
        
        LinearLayout listView;

        if (convertView == null)
        {
        	listView = new LinearLayout(getContext());
            String inflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater layoutInflater =
            		(LayoutInflater) getContext().getSystemService(inflater);
            layoutInflater.inflate(this.mResource, listView, true);

            if( this.isMainActivity){
	            TextView text = (TextView) listView.
	            		findViewById(R.id.id_fragment_main_folder);
	            text.setText(item.getFolderTitle());
            }
            
            LinearLayout imagesContainer= (LinearLayout) listView.
	            		findViewById(R.id.id_fragment_main_imagelist);
          
            ImageDownloader downloader = new ImageDownloader(imagesContainer, 32, 64);
            switch (this.mTab)
            {
            case PHOTOS:
                for (LinkedPhoto photo : item.getItems())
                {
                	downloader.download(photo.getFile(),isMainActivity);
                }
            	break;
            case PANORAMAS:
            	downloader.download(item.getPanorama(),isMainActivity);
            	break;
            }
            
        }
        else
        {
        	listView = (LinearLayout) convertView;
        }

        return listView;
	}
	

	//++++++++++++++++//
	// STATIC METHODS //
	//++++++++++++++++//

	 
    static class ImagesViewHolder
    {
        TextView text;
        LinearLayout imagesContainer;
    }
}
